<?php
 
class Main extends MX_Controller {

	var $includes_path = '/admin';				// path to includes for header and footer
	var $redirect = 'main/dashboard';
	var $num = 10;

	function __construct()
	{
		parent::__construct();
 
	}

	function index()
	{	
 		redirect(site_url($this->redirect));
		 
	}

	function dashboard($days = '')
	{
		if ($this->session->userdata('session_user') && !$this->permission->permissions)
		{
			show_error('Sorry, you do not have permission to administer this website. Please go back or '.anchor('/admin/logout', 'log out').'.');
		}
		
		if (!$this->session->userdata('session_user'))
		{
			redirect('/main/login/'.$this->core->encode($this->uri->uri_string()));
		}
		
			// show any errors that have resulted from a redirect
		if ($days === 'permissions')
		{
			$this->form_validation->set_error('Sorry, you do not have permissions to do what you just tried to do.');
		}
		

		// load model and libs
		$this->load->library('parser');

		// set message
		$output['message'] = '';
	

		// get new orders
	/*	if (@in_array('shop', $this->permission->sitePermissions))
		{
			$this->load->model('shop/shop_model', 'shop');

			if ($newOrders = $this->shop->get_new_orders())
			{
				$output['message'] .= '<p>You have <strong>'.sizeof($newOrders).' new order(s).</strong> You can <a href="'.site_url('/admin/shop/orders').'">view your orders here</a>.</p>';
			}
		}*/
		
	 
		$this->load->view('dashboard', $output);

	}
	
	 function login($redirect = '')
	{
		$output = array();
		$this->load->library('auth');
	
		$output['messageType'] = 'notice';
		$output['message'] = 'Enter any letter and press Login';
	 
		if (!$this->session->userdata('session_admin'))
		{
			if ($_POST)
			{		
				 if ($redirect == '')
				{
					$redirect = $this->redirect;
				}
				else
				{
					$redirect = $this->core->decode($redirect);
				}
				
				// set admin session name, if given
				if ($this->auth->login($this->input->post('username'), $this->input->post('password'), 'session_user'))
				{
					// for use with ce
					if ($this->session->userdata('groupID') != 0 && $this->permission->get_group_permissions($this->session->userdata('groupID')))
					{
						$this->session->set_userdata('session_admin', TRUE);
					}
					
					// update quota
					//$quota = $this->site->get_quota();
					//$this->core->set['quota'] = ($quota > 0) ? (floor($quota / $this->site->plans['storage'] * 100)) : 0;
					//$this->core->update('sites', array('siteID' => $this->siteID));

					redirect($redirect);
				}

				// get error message
				else
				{
		 
					$output['message'] = $this->auth->error;
					$output['messageType'] = 'error' ;
				}
			}	
		}
		else
		{
			if ($redirect != '')
			{
				redirect($redirect);
			}
		}
		
		$this->load->view('index',$output);
		
	}
	function logout($redirect = '')
	{
		// load libs etc
		$this->load->library('auth');
		
		// set redirect to default if not given
		if ($redirect == '')
		{
			$redirect = '';
		}
		else
		{
			$redirect = $this->core->decode($redirect);
		}
		$this->auth->logout($redirect);
	}
 
 
    
}